package com.micro.service.auth.server.login.sms;

import com.micro.service.auth.server.redis.RedisManager;
import com.micro.service.common.base.SmsCode;
import com.micro.service.common.dto.ResultDTO;
import com.micro.service.common.enums.SmsCodePrefixEnum;
import com.micro.service.common.utils.HttprequestUtils;
import com.micro.service.common.utils.PhoneUtils;
import com.micro.service.common.utils.ValidateCodeUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * @author lvxiucai
 * @description
 * @date 2019/7/21
 */
@RestController
@RequestMapping("/sms/v1")
@Slf4j
public class SmsCodeController {

    @Autowired
    private RedisManager vcodeManager;


    @RequestMapping("/code")
    public ResultDTO createCode(HttpServletRequest request, HttpServletResponse response) throws IOException {
        ResultDTO<SmsCode> resultDTO = new ResultDTO<>();
        Map<String,Object> requestMap = HttprequestUtils.getRequestMap(request);
        String mobile = requestMap.get("mobile")==null?null:requestMap.get("mobile").toString();
        String dxToken = requestMap.get("dxToken")==null?null:requestMap.get("dxToken").toString();
        String type = requestMap.get("type")==null?null:requestMap.get("type").toString();
        if(StringUtils.isBlank(mobile)){
            return resultDTO.failure("手机号不能为空");
        }
        String version = request.getHeader("version");
        if("8.1.1".equals(version) && StringUtils.isBlank(dxToken)){
            return resultDTO.failure("必须通过图形验证码才能发送短信");
        }
        //校验手机号是否正确
        if (!PhoneUtils.isPhone(mobile)) {
            return resultDTO.failure("请填入正确的手机号");
        }
        if(StringUtils.isBlank(type)){
            return resultDTO.failure("发送短信的类型不能为空");
        }
        try{
            //生成6位随机字符
            String code = ValidateCodeUtil.getCheckCode();
            String redisKey = "";
            if(SmsCodePrefixEnum.SMS_CODE_PREFIX.getType().equals(type)){
                redisKey = SmsCodePrefixEnum.SMS_CODE_PREFIX.getType()+mobile;
            }else if(SmsCodePrefixEnum.SMS_CODE_BIND_MOBILE_PREFIX.getType().equals(type)){
                redisKey = SmsCodePrefixEnum.SMS_CODE_BIND_MOBILE_PREFIX.getType()+mobile;
            }else {
                return resultDTO.failure("请设置发送短信的类型");
            }

            Object obj = vcodeManager.getVcode(redisKey);
            //校验两分钟内是否已发送
            if(obj != null){
                return resultDTO.failure("请勿重复发送，请在2分钟后重试");
            }
            vcodeManager.saveVcode(redisKey,code,120, TimeUnit.SECONDS);
            // 调用短信接口发送短信
            log.info("发送短信验证码{}至{}",code,mobile);
//            boolean sendResult = SmsSendUtils.sendSms(mobile,code,"2","32497");
//            if(!sendResult){
//                log.error("发送短信验证码至{}失败",mobile);
//            }
            resultDTO.success(new SmsCode(code,120));
            return resultDTO;
        }catch (Exception e){
            log.error("发送短信验证码异常！e:{}",e);
            return resultDTO.failure("发送短信验证码异常!");
        }



    }




}
